import java.util.ArrayList;

public class LearnArrayList {

    public static void main(String[] args) {
        try{
        ArrayList<String> list = new ArrayList(); //使用泛型，可以帮助检查错误，定义的string，后面如果不经意输入了int的，会报错
            list.add("laowang");
            list.add("laowang2");
            list.add("laowang3");
            list.add("laowang4");
            list.add("laowang5");
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
            System.out.println(list.get(3));
            System.out.println(list.get(4));
            System.out.println("");
            list.remove(2);//删除元素
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
            System.out.println(list.get(3));
            System.out.println("");
            list.add(4,"添加的元素");
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
            System.out.println(list.get(3));
            System.out.println(list.get(4));
            System.out.println("");
            list.set(0,"更改第0号元素");//修改
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
            System.out.println(list.get(3));
            System.out.println(list.get(4));
            if(list.contains("更改第0号元素")){ //判断集合中是否有这个东西
                System.out.println("有更改第0号元素");
            }

        }catch(IndexOutOfBoundsException e){
            System.out.println("数组下标越界");
        }
    }
}
